<?php
/**************************************************
* PluginLotto.com                                 *
* Copyrights (c) 2005-2010. iZAP                  *
* All rights reserved                             *
***************************************************
* @author iZAP Team "<support@izap.in>"
* @link http://www.izap.in/
* @version {version} $Revision: {revision}
* Under this agreement, No one has rights to sell this script further.
* For more information. Contact "Tarun Jangra<tarun@izap.in>"
* For discussion about corresponding plugins, visit http://www.pluginlotto.com/pg/forums/
* Follow us on http://facebook.com/PluginLotto and http://twitter.com/PluginLotto
 */

global $CONFIG;
if(!$CONFIG->post_byizap->form_validated) {
  register_error(elgg_echo('izap_poll:error:missing_required_fields'));
  forward($_SERVER['HTTP_REFERER']);
}

$IzapPoll = new izapPoll($CONFIG->post_byizap->attributes['guid'],array('post' => &$CONFIG->post_byizap ));
$IzapPoll->access_id = ACCESS_PUBLIC;

// initialising poll options
if(!$CONFIG->post_byizap->attributes['guid']) {
  $options = $CONFIG->post_byizap->attributes['option'];
  $counter = 1;

  foreach($options as $option) {
    if(!empty ($option)) {
      $option_dec = 'option_'.$counter;
      $voted_option = 'voted_'.$counter;
      $IzapPoll->$option_dec = $option;
      $IzapPoll->$voted_option = 0;
      $counter++;
    }
  }

  // if the options are less than 2
  if($counter < 3) {
    register_error(elgg_echo('izap_poll:error:atleast_two'));
    forward($_SERVER['HTTP_REFERER']);
    exit;
  }
  
  $IzapPoll->total_votes = 0;
  $IzapPoll->total_options = ($counter - 1);
}

if($IzapPoll->save()) {
  system_message(elgg_echo('izap_poll:saved'));
  forward($IzapPoll->getUrl());
}else {
  register_error(elgg_echo('izap_poll:error_saving'));
  forward($_SERVER['HTTP_REFERER']);
}
exit;